import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/store_order_list_vo.dart';

class StoreOrderPageController extends GetxController {
  
  /// tab 下标
  var tabIndex = 0.obs;
  /// 订单状态 1 => 待付款 2 => 待发货（已付款） 5 => 发货中 8 => 已完成 7 => 已取消
  List<int> statusArr = [-1, 1, 5, 8, 7];
  /// 订单数据
  var dataArr = <StoreOrderListVo>[
    StoreOrderListVo(data: [], meta: StoreOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    StoreOrderListVo(data: [], meta: StoreOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    StoreOrderListVo(data: [], meta: StoreOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    StoreOrderListVo(data: [], meta: StoreOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    StoreOrderListVo(data: [], meta: StoreOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
  ].obs;
  /// 是否展示类型选项
  var showSelected = false.obs;
  /// 选中的类型Str
  var typeStr = '全部'.obs;
  /// 选中的类型 -1 全部、0 普通商城、3 星选专区
  var type = '-1'.obs;
  var typeTemp = '-1'.obs;

  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    tabIndex.value = argumentsMap['tabIndex'];
    getStoreOrderList();
    super.onReady();
  }

  /// 获取订单列表
  getStoreOrderList() async {
    // EasyLoading.show();
    // for (int i = 0; i < statusArr.length; i++) {
    //   StoreOrderListVo tempVo = await HttpUtilService.getStoreOrderList(status: statusArr[i], page: dataArr[i].meta!.pagination!.currentPage!, loading: false);
    //   dataArr[i] = tempVo;
    //   update();
    //   EasyLoading.dismiss();
    // }

    // EasyLoading.dismiss();
    // dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    // StoreOrderListVo tempVo = await HttpUtilService.getStoreOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, type: type.value);
    // dataArr[tabIndex.value] = tempVo;
    // update();

    dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    StoreOrderListVo tempVo = await HttpUtilService.getStoreOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, type: type.value, loading: true);
    dataArr[tabIndex.value] = tempVo;
    update();
  }

  /// 加载更多订单列表
  onloadStoreOrderList() async {    
    if (dataArr[tabIndex.value].meta!.pagination!.total! <= dataArr[tabIndex.value].data!.length) {
      return;
    }
    dataArr[tabIndex.value].meta!.pagination!.currentPage = dataArr[tabIndex.value].meta!.pagination!.currentPage! + 1;
    StoreOrderListVo tempVo = await HttpUtilService.getStoreOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, type: type.value, loading: false);
    dataArr[tabIndex.value].data!.addAll(tempVo.data!);
    update();
  }


  /// 获取Tab某项订单列表
  getStoreTabItemOrderList(int index, {bool loading = false}) async {
    dataArr[index].meta!.pagination!.currentPage = 1;
    StoreOrderListVo tempVo = await HttpUtilService.getStoreOrderList(status: statusArr[index], page: dataArr[index].meta!.pagination!.currentPage!, type: type.value, loading: loading);
    dataArr[index] = tempVo;
    update();
  }

  /// 取消订单
  Future<bool> storeOrderCancel(int orderId) async {
    var res = await HttpUtilService.storeOrderCancel(orderId: orderId);
    if (res) {
      getStoreTabItemOrderList(tabIndex.value);
    }
    return res;
  }

  /// 确认收货
  Future<bool> storeOrderReceive(int orderId) async {
    var res = await HttpUtilService.storeOrderReceive(orderId: orderId);
    if (res) {
      EasyLoading.showToast('确认收货成功');
      getStoreTabItemOrderList(tabIndex.value, loading: false);
    }
    return res;
  }

}